function myPromiseAll(promises) {
  return new Promise((resolve, reject) => {
    if (!Array.isArray(promises)) {
      return reject(new TypeError("参数必须是一个数组"))
    }
    if (promises.length === 0) {
      return resolve([])
    }
    const results = new Array(promises.length)
    let completeCount = 0

    promises.forEach((promise, index) => {
      Promise.resolve(promise)
        .then((result) => {
          results[index] = result
          completeCount++
          if (completeCount === promises.length) {
            resolve(results)
          }
        })
        .catch((error) => {
          reject(error)
        })
    })
  })
}

// 测试用例1: 所有Promise都成功
const p1 = Promise.resolve(1)
const p2 = Promise.resolve(2)
const p3 = Promise.resolve(3)

myPromiseAll([p1, p2, p3])
  .then((results) => console.log(results))
  .catch((error) => console.log(error))
// 测试用例2: 有一个Promise失败
const p4 = Promise.resolve(4)
const p5 = Promise.reject("出错了")
const p6 = Promise.resolve(6)

myPromiseAll([p4, p5, p6])
  .then((results) => console.log(results))
  .catch((error) => console.log(error))
// 测试用例3: 非Promise值
myPromiseAll([7, 8, 9, 10])
  .then((results) => console.log(results))
  .catch((error) => console.log(error))
// 测试用例4: 空数组
myPromiseAll([])
  .then((results) => console.log(results))
  .catch((error) => console.log(error))
